home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 05 / 6 / DISK0564.ZIP / SOURCE.ARC / B.ARC / FIXPATH.C < prev    next >
C/C++ Source or Header  |  1986-03-29  |  4KB  |  109 lines

  1. #include "fixpath.h"
  2. #define NULL (void *)0
  3. extern char *index();
  4. extern unsigned int doscall();
  5.  
  6. static struct regstruc { /* used by function 'doscall' */
  7.     unsigned int ax,bx,cx,dx,si,di;
  8. } inregs,outregs;
  9.  
  10. int fixpath(ip,sp,lip)
  11. /*      THIS FUNCTION PARSES A (POSSIBLY AMBIGUOUS) DOS PATH NAME       */
  12. /*      INTO A SEARCH PATH AND A LEAD-IN PATH.  THE SEARCH PATH CAN     */
  13. /*      BE USED AS AN ARGUMENT TO DOS FUNCTIONS 4E AND 4F.  THE         */
  14. /*      LEAD-IN PATH IS INTENDED TO BE PREFIXED TO THE SIMPLE FILENAME  */
  15. /*      RETURNED BY THESE FUNCTIONS, IN ORDER TO PRODUCE A COMPLETE     */
  16. /*      PATH NAME USABLE BY OPEN, RENAME, ETC.                          */
  17. /*                                                                      */
  18. /*      ON RETURN, THE FUNCTION VALUE IS SET AS FOLLOWS:                */
  19. /*          0 IF PATH IS AN UNAMBIGUOUS FILE NAME                       */
  20. /*          1 IF PATH IS AN AMBIGUOUS FILE NAME (E.G. "FOO?.*")         */
  21. /*          2 IF PATH IS A DIRECTORY SPEC (E.G.: A:\FOO\BAR\")          */
  22. /*          3 IF PATH IS A DRIVE SPEC (E.G. "A:") OR NULL STRING        */
  23. /*          4 IF PATH IS A DIRECTORY                                    */
  24. /*          5 IF PATH TYPE UNKNOWN OR DOES NOT EXIST                    */
  25. /*                                                                      */
  26. /*      WRITTEN FOR AZTEC C86, V. 3.20e                                 */
  27. /*      BY JON DART, 1866 DIAMOND ST., SAN DIEGO, CA  92109             */
  28. /*                                                                      */
  29. /*      MODELLED AFTER THE C FUNCTION fixpath() IN DR. DOBB'S JOURNAL,  */
  30. /*      OCT. 1985, PP. 16-18.                                           */
  31.  
  32. char *ip; /* input path */
  33. register char *sp, /* search path */
  34.               *lip; /* lead-in or prefix path */
  35. {
  36.     char *cp, lastch;
  37.     int n,type,len;
  38.  
  39.     len = strlen(ip);
  40.     if ( (len==0)  /* null string */
  41.        || (strcmp(ip+1,":")==0)) { /* d: only */
  42.            type = TYPE_DRV;
  43.     }
  44.     else if ((lastch = ip[len-1])=='\\') { /* ends in backslash */
  45.            type = TYPE_DSP;
  46.     }
  47.     else if (lastch == '.') {
  48.          if (strcmp(ip,"..")==0) {
  49.              type = TYPE_UNK; /* we know ".." is a directory, but we
  50.                                  will make sure such a directory exists,
  51.                                  before returning the type */
  52.          }
  53.          else { /* single dot at end of ip, assume it's a directory spec */
  54.              strcpy(lip,ip);
  55.              strcpy(sp,ip);
  56.              lip[len-1]='\000'; /* remove dot from end of lip */
  57.              sp[len-1] ='\000'; /* remove dot from end of sp */
  58.              strcat(sp,"*.*");
  59.              return(TYPE_DSP);
  60.          }
  61.     }
  62.     if ((type==TYPE_DSP) || (type==TYPE_DRV)) {
  63.         strcpy(lip,ip);
  64.         strcpy(sp,ip);
  65.         strcat(sp,"*.*");
  66.         return(type);
  67.     }
  68.     else { /* use dos call 43 (get attribute) to find type of path */
  69.         inregs.ax = 0;
  70.         inregs.dx = (unsigned int) ip;
  71.         inregs.cx = 0;
  72.         if ( doscall(0x43,&inregs,&outregs) ) { /* failed to parse path */
  73.             if (outregs.ax==0x0003) { /* might be valid (but ambiguous) path */
  74.                 if ((index(ip,'*')!=NULL)
  75.                    || (index(ip,'?')!=NULL)) { /* is ambiguous path name */
  76.                     type = TYPE_AFN;
  77.                 }
  78.                 else /* dos fn. 43 choked, something wrong */
  79.                     type = TYPE_UNK;
  80.             }
  81.             else /* weird error code from dos fn. 43 */
  82.                 type = TYPE_UNK;
  83.         }
  84.         else { /* something corresponding to path exists on disk */
  85.             if (outregs.cx & 0x0010) /* path is directory */
  86.                 type = TYPE_DIR;
  87.             else                  /* path is unambiguous file name */
  88.                 type = TYPE_UFN;
  89.         }
  90.     }
  91.     if (type==TYPE_DIR) { /* directory */
  92.         strcpy(sp,ip);
  93.         strcpy(lip,ip);
  94.         strcat(sp,"\\*.*");
  95.         strcat(lip,"\\");
  96.     }
  97.     else { /* AFN, UFN or UNK */
  98.          strcpy(sp,ip);
  99.          cp = sp+len-1;
  100.          for(;cp>sp;--cp)
  101.              if (('\\'==*cp) || (':'==*cp)) break;
  102.          if (cp>sp) ++cp; /* retain colon/slash */
  103.          *cp='\000';
  104.          strcpy(lip,sp);
  105.          strcpy(sp,ip);
  106.     }
  107.     return(type);
  108. }
  109.